package org.springframework;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class EnvironmentTest {

	Logger log = LoggerFactory.getLogger(BeanDefinitionTest.class);

	@Test
	public void testMoreEnvProperties() throws IOException {
		ApplicationContext ctx = new GenericApplicationContext();
		StandardEnvironment env = (StandardEnvironment) ctx.getEnvironment();
		boolean containsMyProperty = env.containsProperty("JAVA_HOME");
		log.info("Does my environment contain the 'JAVA_HOME' property? {}", containsMyProperty);
		log.info("The 'JAVA_HOME' property value is {}", env.getProperty("JAVA_HOME"));
	}

	@Test
	public void testSystemProperties() {
		// jvm的信息
		Properties properties = System.getProperties();
		Set<Map.Entry<Object, Object>> entries = properties.entrySet();
		for (Map.Entry<Object, Object> entry : entries) {
			log.info("{} - {}", entry.getKey(), entry.getValue());
		}
	}

	@Test
	public void testSystemEnv() {
		//系统环境变量
		Map<String, String> env = System.getenv();
		Set<Map.Entry<String, String>> entries = env.entrySet();
		for (Map.Entry<String, String> entry : entries) {
			log.info("{} - {}", entry.getKey(), entry.getValue());
		}
	}

	@Test
	public void testCreateEnv() {
		ApplicationContext ctx = new AnnotationConfigApplicationContext("org.framework");
		StandardEnvironment env = (StandardEnvironment) ctx.getEnvironment();
		MutablePropertySources propertySources = env.getPropertySources();

		// 自定义属性
		Properties properties = new Properties();
		properties.setProperty("color", "red");
		propertySources.addFirst(new PropertiesPropertySource("my-properties", properties));

		log.info("Does my environment contain the 'color' property? {}", env.getProperty("color"));
		log.info("Does my environment contain the 'my-name' property? {}", env.getProperty("my-name"));
	}

	@Test
	public void testProfile() {

		// 1、创建上下文
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		// 2、配置环境
		ConfigurableEnvironment environment = ctx.getEnvironment();
		environment.setActiveProfiles("test");
		ctx.setEnvironment(environment);

		// 3、扫描
		ctx.scan("com.ydlclass");
		// 4、刷新容器
		ctx.refresh();
		String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
		log.info("beanDefinitionNames -> {}", Arrays.toString(beanDefinitionNames));

		// 5、获取一个bean
		User dog = ctx.getBean(User.class);
		log.info("dog: {}", dog);
	}
}
